<?php
/**
 * Created by PhpStorm.
 * User: 胥九庆
 * Date: 2023/01/04
 * Time: 16:37
 */
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\CourseComment;
use app\common\model\CourseHit;
use app\common\model\CourseOrder as CourseOrderModel;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\Response;
use app\common\model\Course as CourseModel;

class Course extends Base
{
    /**
     * 课程列表
     */
    public function getList(): Response
    {
        $param = $this->request->param();
        $map['is_del'] = 0;
        $map['status'] = 1;
        foreach ($param as $key => $value) {
            if (!in_array($key, ['page', 'limit']) && $value) {
                $map[$key] = $value;
            }
        }
        $list = CourseModel::getPageList($map, 'id,title,img,price,describe,is_free,type', $this->getPage(), $this->getLimit(),false, [], [], ['key' => 'sort', 'value' => 'asc']);
        foreach ($list['data'] as &$item) {
            $item->is_free_name = '免费';
            if ($item->is_free == 2) {
                $count = CourseOrderModel::where(['course_id' => $item->id, 'uid' => $this->user->id, 'state' => 2, 'is_del' => 0])->count('id');
                $item->is_free_name = $count <= 0 ? '付费' : '已购买';
            }
        }
        return Result::fastReturn($list);
    }

    /**
     * 验证课程
     */
    public function verify(): Response
    {
        $param = $this->request->get();
        $info = CourseModel::where(['status' => 1, 'is_del' => 0, 'id' => $param['id']])->field('id,cate,is_free')->find();
        if (empty($info)) return Result::fail('课程不存在');
        $is_read = 1;
        if ($info->is_free == 2) {
            $count = CourseOrderModel::where(['course_id' => $param['id'], 'uid' => $this->user->id, 'state' => 2, 'is_del' => 0])->count('id');
            if ($count <= 0) $is_read = 2;
        }
        if ($info->cate == 3 && $this->user->level_id < 3) {
            $is_read = 3;
        }
        $result['is_read'] = $is_read;

        return Result::success('success', $result);
    }

    /**
     * 课程详情
     */
    public function details(): Response
    {
        $param = $this->request->get();
        $info = CourseModel::where(['status' => 1, 'is_del' => 0, 'id' => $param['id']])
            ->field('id,title,describe,is_free,price,img,type,link,content,total_hit,total_comment')->find();
        if (empty($info)) return Result::fail('课程不存在');
        /*$url = 'https://' . $_SERVER['SERVER_NAME'] . '/uploads/';
        $info->content = htmlspecialchars_decode(str_replace('/uploads/', $url, $info->content));
        $info->content = str_replace('\\', '', $info->content);*/
        $info->content = handleRichText($info->content);
        $info->link = $info->link? config('qiniu')['domain'].$info->link: '';
        $info->is_hit = CourseHit::where(['course_id' => $info->id, 'uid' => $this->user->id])->count('id');

        return Result::success('success', $info);
    }

    /**
     * 课程点赞
     */
    public function hit() :Response
    {
        $param = $this->request->post();
        $info =  CourseModel::where(['id' => $param['course_id'], 'is_del' => 0])->find();
        if (empty($info)) return Result::fail('课程不存在');

        return Result::fastReturn(CourseHit::hit($param, $this->user->id));
    }

    /**
     * 课程评论
     */
    public function comment() :Response
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'CourseComment.add');
        } catch (ValidateException $e) {

            return Result::fail($e->getError());
        }
        $info =  CourseModel::where(['id' => $param['course_id'], 'is_del' => 0])->find();
        if (empty($info)) return Result::fail('课程不存在');

        return Result::fastReturn(CourseComment::add($param, $this->user->id));
    }

    /**
     * 课程评论列表
     */
    public function commentList(): Response
    {
        $param = $this->request->param();
        $map['is_del'] = 0;
        foreach ($param as $key => $value) {
            if (!in_array($key, ['page', 'limit']) && $value) {
                $map[$key] = $value;
            }
        }
        $result = CourseComment::getPageList($map, 'id,uid,course_id,comment,create_time', $this->getPage(), $this->getLimit(),false, ['user']);
        foreach ($result['data'] as $val) {
            $val->nickname = $val->user->nickname;
            $val->photo = $val->user->photo;
            unset($val->user);
        }

        return Result::fastReturn($result);
    }
}